package kr.saeildamil.androidstudty.ch16;

import java.util.Calendar;

import kr.saeildamil.androidstudty.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Ch16AlertDialog extends Activity {
	final static int SAMPLEDLG=0;
	final static int QUESTIONDLG=1;
	
	int a=3;
	int b=4;
	int result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_alertdialog);
	}
	
	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.ch16_alertdialog_btn01:
			AlertDialog.Builder bld01=new AlertDialog.Builder(this);
			bld01.setTitle("알립니다");
			bld01.setMessage("대화상자를 열었습니다");
			bld01.setIcon(R.drawable.ic_launcher);
			bld01.show();
			break;
		case R.id.ch16_alertdialog_btn02:
			AlertDialog.Builder bld02=new AlertDialog.Builder(this);
			bld02.setTitle("알립니다2");
			bld02.setMessage("대화상자를 열었습니다");
			bld02.setIcon(R.drawable.ic_launcher);
			bld02.setNegativeButton("닫기", null);
			bld02.show();
			break;
		case R.id.ch16_alertdialog_btn03:
			AlertDialog.Builder bld03=new AlertDialog.Builder(this);
			bld03.setTitle("알립니다3");
			bld03.setMessage("대화상자를 열었습니다");
			bld03.setIcon(R.drawable.ic_launcher);
			bld03.setNegativeButton("Negative", null);
			bld03.setNeutralButton("Neutral", null);
			bld03.show();
			break;
		case R.id.ch16_alertdialog_btn04:
			showDialog(SAMPLEDLG);
			break;
		case R.id.ch16_alertdialog_btn05:
			showDialog(QUESTIONDLG);
			break;
		case R.id.ch16_alertdialog_btn06:
			showModal();
			break;
		case R.id.ch16_alertdialog_btn07:
			new AlertDialog.Builder(this)
			.setTitle("질문")
			.setMessage("어떤 연산을 하시겠습니까?")
			.setPositiveButton("덧셈", listener)
			.setNegativeButton("곱셉", listener);
			break;
		}//end of switch
	}//end of method mOnClick
	
	DialogInterface.OnClickListener listener=new OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			if(which==DialogInterface.BUTTON_POSITIVE){
				result=a+b;
			}else{
				result=a*b;
			}
			
			TextView tv=(TextView) findViewById(R.id.ch16_alertdialog_tv);
			tv.setText("연산 결과는 "+result);
			Toast.makeText(Ch16AlertDialog.this, "연산완료", Toast.LENGTH_SHORT).show();
		}
	};
	
	private void showModal(){
		try{
			Thread.sleep(3000);
		}catch(Exception e){}
		
		boolean stat=true;//예외 발생
		if(stat){
			new AlertDialog.Builder(this)
			.setMessage("해당 이유로 에러 발생")
			.setPositiveButton("종료", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();//액티비티 종료
				}
			})
			.show();
		}else{
			Toast.makeText(this, "정상 완료", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		if(id==SAMPLEDLG){
			return new AlertDialog.Builder(this)
			.setTitle("알림")
			.setMessage("대화상자 오픈")
			.setIcon(R.drawable.ic_launcher)
			.setPositiveButton("닫기", null)
			.setCancelable(false)
			.create();
		}else{
			return new AlertDialog.Builder(this)
			.setTitle("질문")
			.setMessage("밥 먹었나요?")
			.setPositiveButton("먹었음", null)
			.setNegativeButton("굶었다", null)
			.create();
		}
		
	}

	@Override
	@Deprecated
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		switch (id) {
		case SAMPLEDLG:
			break;
		case QUESTIONDLG:
			Calendar cal=Calendar.getInstance();
			String sTime=String.format("%d시%d분%d초",cal.get(Calendar.HOUR),
					cal.get(Calendar.MINUTE),
					cal.get(Calendar.SECOND));
			dialog.setTitle("ㅎㅎㅎㅎㅎ");
			break;
		}
	}
}
